package com.算法4.base;

import java.util.Arrays;

/**
 * @description: TODO 类描述
 * @author: YUANG
 * @date: 2022/3/3
 **/
public class SumTest {
    public static void main(String[] args) {
//        int[] a = {1, -1, 3, -3, 5, 6, -5, -6};
//        System.out.println(twoSumCount(a));

        int[] a = {0, 1, -1, 2, -2};
        System.out.println(threeSumCount(a));

    }

    public static int twoSumCount(int[] a) {
        Arrays.sort(a);
        int n = a.length;
        int cnt = 0;
        for (int i = 0; i < n; i++) {
            if (Arrays.binarySearch(a, -a[i]) > i) {
                cnt++;
            }
        }
        return cnt;
    }

    public static int threeSumCount(int[] a) {
        Arrays.sort(a);

        int n = a.length;
        int cnt = 0;
        for (int i = 0; i < n; i++) {
            for (int j = i + 1; j < n; j++) {
                if (Arrays.binarySearch(a, -(a[i] + a[j])) > j) {
                    cnt++;
                }
            }
        }
        return cnt;
    }
}
